<template>
  <div>
    <!-- <p>{{userInfo}}</p>  -->
    <div class="topbar">
      <div class="wrap wrapTopbar">
        <div class="l">欢迎来到叩丁狼积分商城</div>
        <ul class="r">
          <li>
            <img :src="userInfo.headImg" class="avator" alt="" />
            用户名：{{ userInfo.nickName }}
          </li>
          <li>我的积分：{{ userInfo.coin }}</li>
          <li>获取积分</li>
          <li>叩丁狼官网</li>

          <li class="cart-btn" v-if="isLogined">
            <img src="../assets/img/cart.png" alt="" />
            <span>购物车</span>
            <strong>{{ cartTotal }}</strong>
          </li>
          <li class="login-btn" @click="changeIsShowLoginModal(true)" v-else>
            登录
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations, mapState } from "vuex";
import { WXLoginAPI } from "@/request/api";
export default {
  created() {
    // console.log("执行钩子函数*****8");
    //只要用户扫码了，页面会重新刷新 ，此时可以获取下code值
    //[!!!有可能会获取不到code...$nextTick()!!!!]
    this.$nextTick(async () => {
      // console.log("this.$route", this.$route.query.code);
      let mycode = this.$route.query.code;
      if (!mycode) {
        let mytoken = localStorage.getItem("x-auth-token");
        this.changeIsLogin(Boolean(mytoken));
        if (mytoken) {
          // console.log("更新用户信息");
          this.asyncChangeUserInfo();
        } else {
          // console.log("初始化用户信息");
          this.initUserInfo();
        }
        return;
      }
      //当有code时，就可以发起微信扫码登录请求
      let res = await WXLoginAPI({
        code: mycode,
      });
      // console.log("WXLoginAPI:", res);
      //0 400 407
      if (res.code == 0) {
        //1 . 提示登录成功
        alert("登录成功");
        //2. 存储token
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        //3. 改变登录状态 （切换购物车显示）
        this.changeIsLogin(true);
        // 6. 清楚地址栏的code
        this.$router.push(this.$route.path);
          // 7. 获取用户信息
        this.asyncChangeUserInfo();
        // console.log(this.$route.path);
      } else if (res.code == 400) {
        //1. 提示二维码失效
        alert("请重新扫码二维码");
        //2. 把登录模态框打开
        this.changeIsShowLoginModal(true);
      } else if (res.code == 407) {
        //1.提示用户再使用手机号验证码登录一次
        alert("请使用手机号验证码登录一次");
        //2. 把登录模态框打开
        this.changeIsShowLoginModal(true);
        //3. 存储uuid （登录时取）
        localStorage.setItem("uuid", res.uuid);
      }
    });
  },
  data() {
    return {
      //购物车总数
      // cartTotal: 5,
    };
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.isLogined.isLogined,
      cartTotal: (state) => state.userInfo.cartTotal,
      userInfo: (state) => state.userInfo.userInfo,
    }),
  },
  // watch: {
  //   // 方式一:
  //   "$route.path": {
  //     // handler(newVal, oldVal) {
  //     //   console.log("$route.path:", newVal, oldVal);
  //     //   let mytoken = localStorage.getItem("x-auth-token");
  //     //   // if (mytoken) {
  //     //   //   console.log("登录状态 true");
  //     //   //   this.changeIsLogin(true);
  //     //   // } else {
  //     //   //   console.log("登录状态 false");
  //     //   //   this.changeIsLogin(false);
  //     //   // }
  //     //   // 简化代码
  //     //   this.changeIsLogin(Boolean(mytoken));
  //     // },
  //   },
  // },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      chanIsShowToast: "showToast/chanIsShowToast",
      changeIsLogin: "isLogined/changeIsLogin",
      
      initUserInfo: "userInfo/initUserInfo",
      showToastFn() {
        this.chanIsShowToast(true);
        setTimeout(() => {
          this.chanIsShowToast(false);
        }, 1500);
      },
    }),
    ...mapActions({
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    })
  },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  background-color: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  color: #fffefe;
  line-height: 40px;
  .wrapTopbar {
    display: flex;
    justify-content: space-between;
    .r {
      display: flex;
      justify-content: space-between;
      li {
        margin: 0 10px;
        cursor: pointer;
        &.login-btn {
          width: 124px;
          height: 40px;
          text-align: center;
          background: #0a328e;
          // box-sizing: border-box;
        }
      }
      .avator {
        width: 22px;
        vertical-align: middle;
        margin-right: 10px;
        border-radius: 13px;
      }
      .cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        img {
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
    }
  }
}
</style>
